package com.polaris.live.ui.vip.activity

import android.graphics.Canvas
import android.graphics.Paint
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.ColorUtils
import com.polaris.live.adapter.vip.VipDetailAdapter
import com.polaris.live.common.constant.CommonConst
import com.polaris.live.common.ext.autoDp
import com.polaris.live.common.mvvm.base2.BaseVbActivity
import com.polaris.live.common.util.ImmersionBarBuilder
import com.polaris.live.databinding.ActivityVipDetailBinding
import com.polaris.live.manager.AppConfigManager
import com.polaris.live.utils.image.ImageUtils
import com.polaris.live.utils.route.RouterUtils
import com.polaris.live.viewmodel.VipDetailViewModel

/**
 * VipDetailActivity vip明细
 *
 * @author Created by 天晴 on 2024/12/10 10:02
 * @since 1.0.0
 **/
class VipDetailActivity : BaseVbActivity<VipDetailViewModel, ActivityVipDetailBinding>() {

    private val mAdapter by lazy { VipDetailAdapter() }
    override fun ImmersionBarBuilder.buildImmersionBar() {
        setTitleBar(mBinding.bvTitle)
        attachImmersionBar { transparentStatusBar() }
    }

    override fun initView(savedInstanceState: Bundle?) {
        mViewModel.onRefresh()

        mBinding.recyclerView.apply {
            recycler.layoutManager = LinearLayoutManager(context)
            recycler.addItemDecoration(object : RecyclerView.ItemDecoration() {
                override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
                    super.onDraw(c, parent, state)
                    val childCount = parent.childCount
                    for (i in 0 until childCount - 1) {
                        val childView = parent.getChildAt(i)

                        val params = childView.layoutParams as RecyclerView.LayoutParams
                        val left = childView.left - params.leftMargin
                        val right = childView.right + params.rightMargin
                        val top = childView.bottom + params.bottomMargin
                        val bottom = top + 1.autoDp

                        val paint = Paint().apply {
                            color = ColorUtils.getColor(com.polaris.live.R.color.color_e8e8e8)
                            style = Paint.Style.FILL
                        }
                        c.drawRect(left.toFloat(), top.toFloat(), right.toFloat(), bottom.toFloat(), paint)
                    }
                }

            })
            recycler.itemAnimator = null
            attachedAdapter(mAdapter, mViewModel).observe(
                this@VipDetailActivity,
                mViewModel.vipDetailLiveData
            )
        }
        initListener()
    }

    private fun initListener() {
        mBinding.bvTitle.setOnRightClientListener {
            val safeUrl = AppConfigManager.getConfig(CommonConst.Configs.customerServiceUrl)
            RouterUtils.routerWeb(ImageUtils.imgPath(safeUrl))

        }
    }

}